package com.easylive.utils;
import com.easylive.entity.constants.Constants;
import com.easylive.exception.BusinessException;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;


public class StringTools {

    public static void checkParam(Object param) {
        try {
            Field[] fields = param.getClass().getDeclaredFields();
            boolean notEmpty = false;
            for (Field field : fields) {
                String methodName = "get" + StringTools.upperCaseFirstLetter(field.getName());
                Method method = param.getClass().getMethod(methodName);
                Object object = method.invoke(param);
                if (object != null && object instanceof String && !StringTools.isEmpty(object.toString())
                        || object != null && !(object instanceof String)) {
                    notEmpty = true;
                    break;
                }
            }
            if (!notEmpty) {
                throw new BusinessException("多参数更新，删除，必须有非空条件");
            }
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("校验参数是否为空失败");
        }
    }

    public static String upperCaseFirstLetter(String field) {
        if (isEmpty(field)) {
            return field;
        }
        //如果第二个字母是大写，第一个字母不大写
        if (field.length() > 1 && Character.isUpperCase(field.charAt(1))) {
            return field;
        }
        return field.substring(0, 1).toUpperCase() + field.substring(1);
    }

    public static boolean isEmpty(String str) {
        if (null == str || "".equals(str) || "null".equals(str) || "\u0000".equals(str)) {
            return true;
        } else if ("".equals(str.trim())) {
            return true;
        }
        return false;
    }

    public static String encodePassword(String password) {
        return DigestUtils.md5Hex(password);
    }

    public static String getRandomNumber(Integer length) {
        return RandomStringUtils.random(length, false, true);
    }

    public static String getRandomString(Integer length) {
        return RandomStringUtils.random(length, true, false);
    }

    /**
     * 判断路径是否合法，不合法的肯定是调接口的
     * @param path
     * @return
     */
    public static boolean pathIsOk(String path) {
        if (StringTools.isEmpty(path)) {
            return true;
        }
        if (path.contains("../") || path.contains("..\\")) {
            return false;
        }
        return true;
    }

    /**
     * 获取文件后缀名
     * @param originalFilename
     * @return
     */
    public static String getFileSuffix(String originalFilename) {
        if (StringTools.isEmpty(originalFilename) || originalFilename.lastIndexOf(".") == -1) {
            throw new BusinessException("非法文件");
        }
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (!Arrays.asList(".jpeg", ".jpg", ".svg", ".png", ".webp").contains(suffix)) {
            throw new BusinessException("文件格式有误");
        }
        return suffix;
    }

    /**
     * 校验文件是否合法。然后生成一个随机30位字符串作为文件名
     * @param originalFilename
     * @return
     */
    public static String getImageFileName(String originalFilename) {
        String fileSuffix = getFileSuffix(originalFilename);
        return getRandomString(Constants.LENGTH_30) + fileSuffix;
    }
}
